<template>
  <basic-dialog
      v-model="dialog.showDialog"
      :loading="dialog.loading"
      :title="dialog.title"
      append-to-body
      width="900"
      @cancel="cancelDialog"
      @submit="handleSubmit"
  >
    <!-- 使用新的基础组件 -->
    <basic-crud
        ref="crudRef"
        :apiGetListUrl="props.apiGetUrl"
        :apiListParams="apiListParams"
        :option="tableOption"
        apiGetListMethod="post"
        selectType="radio"
        @select="handleSelect"
        @row-click="handleRowClick"
        @search-change="handleSearch"
        @search-reset="handleReset"
        @sort-change="handleSort"
    >
    </basic-crud>
  </basic-dialog>
</template>

<script setup>
import {
  getTechnologyList,
  postWorkOrderTargetById,
  putWorkOrderTargetById,
  getListReplace
} from "../../../apis/generate/productionPlan";

const props = defineProps({
  apiGetUrl: {
    type: String,
    default: '/mpm/processSpecificationController/list'
  }
});
const emit = defineEmits(['refresh']);
const dialog = reactive({
  showDialog: false,
  title: '选择规程',
  loading: false
});
const paramsForm = ref({});
const crudRef = ref(null);
const tableOption = reactive({
  addBtn: false,
  menu: false,
  editBtn: false,
  delBtn: false,
  height: '300px',
  headerAlign: 'left',
  align: 'left',
  border: false,
  searchMenuSpan: 6,
  column: [
    {
      label: '规程编号',
      prop: 'code',
      search: true,
      searchSpan: 8,
      sortable: true
    },
    {
      label: '规程名称',
      prop: 'name',
      search: true,
      searchSpan: 8
    },
    {
      label: '规程版本',
      prop: 'rev'
    }
  ]
});
// 打开对话框
const openDialog = (params) => {
  console.log(params);
  dialog.showDialog = true
  paramsForm.value = params;
}

const apiListParams = computed(() => {
  return {
    productCode: paramsForm.value.productCode,
    id: paramsForm.value.targetId
  }
});

const handleSubmit = async () => {
  console.log(paramsForm.value)
  const {type, workOrderId, primaryTargetId} = paramsForm.value;
  const baseForm = {id: workOrderId};

  try {
    if (type === 'plan') {
      await postWorkOrderTargetById({...baseForm, primaryTargetId});
      ElMessage.success("设置规程成功！");
    } else if (type === 'edit') {
      await putWorkOrderTargetById({...baseForm, targetId: primaryTargetId});
      ElMessage.success("更新规程成功！");
    } else {
      await postWorkOrderTargetById({...baseForm, primaryTargetId});
      ElMessage.success("新设置规程成功！");
    }

    cancelDialog();
    this.$emit("refresh");
  } catch (error) {
    console.log(error);
  }
};

const handleSort = (val) => {
  const ordermethod = val.order === 'descending' ? 'desc' : 'asc'
  apiListParams.value = {
    ...apiListParams.value,
    orderBy: val.prop,
    orderMethod: ordermethod
  };

}
const handleRefresh = () => {
  crudRef.value.getList();
}
const cancelDialog = () => {
  dialog.showDialog = false;
}
const handleSelect = (row) => {
  paramsForm.value.primaryTargetId = row[0].id;
};
defineExpose({
  openDialog
})

</script>

<style lang="scss" scoped>

</style>
